package tw.edu.tku.im.maze.database;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;

import tw.edu.tku.im.maze.EndGameRep;
import tw.edu.tku.im.maze.EndGameReq;
import tw.edu.tku.im.maze.MazeMap;
import tw.edu.tku.im.maze.Member;
import tw.edu.tku.im.maze.Packet;
import tw.edu.tku.im.maze.UpdateRoomRep;

public class EndGameProcessor extends PacketProcessor {

   @Override
   String acceptName() {
      return "endGame";
   }

   @Override
   Packet process(Packet packet) {
      EndGameReq req = (EndGameReq)packet;
      int roomID = req.getRoomID();
      try {
         Connection con = DriverManager.getConnection(address,user,passwd);
         con.setCatalog(database);
         CallableStatement cstmt = 
               con.prepareCall("{call usp_endGame(?,?,?,?)}");
         cstmt.setInt(1, roomID);
         cstmt.setString(2, req.getUserID());
         cstmt.setInt(3, req.getTicket());
         cstmt.registerOutParameter(4, Types.INTEGER);
         cstmt.execute();
         return new EndGameRep(cstmt.getInt(4));
      } catch (SQLException e) {
         e.printStackTrace();
      }
      //...... to do something
      EndGameRep rep = new EndGameRep(0);
      return rep;
   }

}
